package com.api.qfengx.data.doc;

import com.alibaba.fastjson.JSON;
import com.weaver.qfengx.*;
import com.weaver.qfengx.entity.Result;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import weaver.hrm.User;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import java.util.List;

@Path("/qfengx/data/doc")
public class DataDocApi {

    private static final LogUtils log = new LogUtils(DataDocApi.class);

    @POST
    @Path("/upload")
    @Produces(MediaType.APPLICATION_JSON)
    public String uploadforoa(@Context HttpServletRequest request,
                              @Context HttpServletResponse response) {
        try {
            log.writeLog("======> 处理接口 ===> 文档上传");
            DiskFileItemFactory factory = new DiskFileItemFactory();
            ServletFileUpload upload = new ServletFileUpload(factory);
            List<FileItem> files = upload.parseRequest(request);
            String categroyid = "";
            String userid = "";
            String name = "";
            FileItem uploadItem = null;
            for (FileItem fileItem : files) {
                switch (fileItem.getFieldName()) {
                    case "filename":
                        uploadItem = fileItem;
                        break;
                    case "category":
                        categroyid = IOUtils.string(fileItem.getInputStream());
                        break;
                    case "name":
                        name = IOUtils.string(fileItem.getInputStream());
                        break;
                    case "userid":
                        userid = IOUtils.string(fileItem.getInputStream());
                        break;
                }
            }

            log.writeLog(String.format("name => %s, userid => %s, categroyid => ", name, userid, categroyid));
            return handleFile(uploadItem, userid, categroyid, name);
        } catch (Exception e) {
            e.printStackTrace();
            return JSON.toJSONString(Result.exception(e.toString()));
        }
    }

    private String handleFile(FileItem uploadItem, String userid, String categroyid, String filename) {
        try {

            // 将文件写入到文档中
            int docid = ImageFileUtils.uploadFile(uploadItem.getInputStream(), User.getUser(NumberUtils.parseInt(userid), 0),
                    categroyid, filename);
            DaoUtils.executeUpdate("update docdetail set doctype = 2 where id = ?", docid);
            DaoUtils.executeUpdate("update docimagefile set isextfile = ? where docid = ?", "", docid);
            return JSON.toJSONString(Result.ok(docid));
        } catch (Exception e) {
            e.printStackTrace();
            return JSON.toJSONString(Result.exception(e.toString()));
        } finally {
            log.writeLog("======> 完成文件上传");
        }
    }

}
